/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ticketmachine.logic;

import java.util.Date;

import ticketmachine.logic.paymentAdapters.Payment;
import ticketmachine.logic.paymentAdapters.PaymentFactory;
import ticketmachine.logic.priceStrategies.IPriceCalculatorStrategy;
import ticketmachine.logic.priceStrategies.PriceCalculatorFactory;
import ticketmachine.logic.taxCalculatorAdapters.TaxCalculatorFactory;

/**
 *
 * @author oiiv2253
 */
public class Transaction {

    int id;
    int transactionType;
    Date date;
    Ticket ticket;
    

    public Transaction(int transactionType) {
        this.transactionType = transactionType;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Ticket getTicket() {
        return ticket;
    }

    public void setTicket(Ticket newTicket) {
        this.ticket = newTicket;
    }

    public int getTransactionType() {
        return transactionType;
    }

    public void setTransactionType(int type) {
        this.transactionType = type;
    }

    public void createNewTicket(TicketDescription ticketType, Station source) {
    	ticket = new Ticket(ticketType, source);
    }
    
    public float calculateTicketPrice(){

        //TEST

        System.out.println("Calculating ticket price");

    	String pricingStrategy=ticket.getDescription().getPricingStrategyClass();
    	IPriceCalculatorStrategy  pc=PriceCalculatorFactory.getInstance().getPriceCalculatorStrategy(pricingStrategy);
    	float price= pc.calculatePrice(ticket);
    	ticket.setPrice(price);
    	TaxCalculatorFactory.getInstance().getTaxCalculatorAdapter().addTaxesToTicket(ticket);
     	return price;
    }
    
    public float applyPromotionToTicket(Promotion promo){
    	ticket.setPromotion(promo);
        //System.out.println(ticket.getPromotion().getName());
        //System.out.println(ticket.getPromotion().getId());
    	String discountStrategy=promo.getDiscountStrategyClass();
    	IPriceCalculatorStrategy  pc=PriceCalculatorFactory.getInstance().getPriceCalculatorStrategy(discountStrategy);
    	float price=pc.calculatePrice(ticket);
    	ticket.setPrice(price);
    	TaxCalculatorFactory.getInstance().getTaxCalculatorAdapter().addTaxesToTicket(ticket);
    	return price;
    }
    
    public float makeNewCashPayment(float amountDue, float amountIntroduced){
    	Payment p=PaymentFactory.getInstance().getCashPayment(amountDue, amountIntroduced);
    	return checkPaymentAuthorization(p);
    }

    public float makeNewCreditPayment(String cardNumber, String ExpiryDate, float amount) {
        Payment p = PaymentFactory.getInstance().getCreditPayment(cardNumber, ExpiryDate, amount);
        return checkPaymentAuthorization(p);
    }

    public float makeNewDebitPayment(String pinNumber, String cardNbr, float amount) {
        Payment p = PaymentFactory.getInstance().getDebitPayment(pinNumber, cardNbr, amount);
        return checkPaymentAuthorization(p);
    }

    public float makeECashPayment(String pin, float amount) {
        Payment p = PaymentFactory.getInstance().getECashPayment(pin, amount);
        return checkPaymentAuthorization(p);
    }
    
    public float checkPaymentAuthorization(Payment p){
    	return p.authorise();
    }


}
